package com.fy.fyzf.utils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtils {

    /**
     * 获取当前日期是星期几<br>
     *
     * @param date
     * @return 当前日期是星期几
     */
    public static  String getWeekOfDate(Date date) {
        String[] weekDays = { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" };
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
        if (w < 0)
            w = 0;
        return weekDays[w];
    }

    public static  String formatDate(String data){
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        Date strtodate;
        String datas = "";
        try {
            strtodate = formatter.parse(data);
            datas =  formatter.format(strtodate);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        return datas;
    }

    /**
     * Date时间转为string
     * @param time
     * @param type type 0-年月日时分秒 1-年月日 2-日 3-时(24)
     * @return
     */
    public static String DateChangeString(Date time,String type) {
        SimpleDateFormat sdf;
        if(type.equals("0")){
            sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        }else if(type.equals("1")){
            sdf = new SimpleDateFormat("yyyy-MM-dd");
        }else if(type.equals("2")){
            sdf = new SimpleDateFormat("dd");
        }else{
            sdf = new SimpleDateFormat("HH");
        }
        return sdf.format(time);
    }
    /**
     * string时间转为Date
     * @param time
     * @param type 0-年月日时分秒 1-年月日
     * @return
     */
    public static Date StringChangeDate(String time,String type) {
        SimpleDateFormat sdf;
        if(type.equals("0")){
            sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        }else{
            sdf = new SimpleDateFormat("yyyy-MM-dd");
        }
        Date date = null;
        try {
            date = sdf.parse(time);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }


    /**
     * 比较两个日期是否是同一天
     * @param time1
     * @param time2
     * @return
     */
    public static Boolean DateCompare(Date time1,Date time2) {
        String date1 = DateChangeString(time1,"1");
        String date2 = DateChangeString(time2,"1");
        System.out.println("date1："+date1+"，date2："+date2+"，比较两个时间："+date1.equals(date2));
        if(date1.equals(date2)){
            return true;
        }
        return false;
    }

    /**
     * 判断日期是否早于今天
     * @param time
     * @return
     */
    public static Boolean DateCompare(Date time) {
        Calendar calendar = Calendar.getInstance();
        int now_y = calendar.get(Calendar.YEAR);//得到年份
        int now_m = calendar.get(Calendar.MONTH)+1;//得到月份
        int now_d = calendar.get(Calendar.DATE);//得到月份中今天的号数
//	        int now_h = calendar.get(Calendar.HOUR_OF_DAY);//得到一天中现在的时间，24小时制
//	        int now_mm = calendar.get(Calendar.MINUTE);//得到分钟数
//	        int now_s = calendar.get(Calendar.SECOND);//得到秒数
        System.out.println("现在是："+now_y+"-"+now_m+"-"+now_d );
        calendar.setTime(time);
        int y = calendar.get(Calendar.YEAR);//得到年份
        int m = calendar.get(Calendar.MONTH)+1;//得到月份
        int d = calendar.get(Calendar.DATE);//得到月份中今天的号数
        System.out.println("时间是："+y+"-"+m+"-"+d);
        if(now_y<y||(now_y==y&&now_m<m)||(now_y==y&&now_m==m&&now_d<=d)) {
            System.out.println("不早于今天");
        }else {
            System.out.println("早于今天");
            return true;
        }

        return false;
    }

    private int compDate(String str) throws ParseException {
        SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date();
        return date.compareTo(sd.parse(str));
    }

    /**
     * 判断当前时间是否在某个时间之前
     *
     * @param tagDateTime 判断的标准
     * @return true是，false不是
     */
    public static boolean belongCalendarBefore(String tagDateTime) {
        try {
            DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Calendar contCalendar = Calendar.getInstance();
            Calendar tagCalendar = (Calendar) contCalendar.clone();
            tagCalendar.setTime(dateFormat.parse(tagDateTime));

            return contCalendar.before(tagCalendar);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return false;
    }
}
